import networkx as nx

from topo1 import build_hybrid_topology_shared_tor
def find_electric_paths(G, s, d, k_paths):
    # Step 1: 创建子图并添加所有节点
    G_electric = nx.Graph()
    G_electric.add_nodes_from(G.nodes())  # 确保所有节点存在
    print(f"构建子图electric_paths: {G_electric.nodes()}")

    # 输出拓扑中所有的边
    for u, v, attr in G.edges(data=True):
        if attr.get('type') == 'electrical':
            G_electric.add_edge(u, v, **attr)
        print(f"边: {u}-{v}, 属性: {attr}")

    # 输出G_electric的节点和边
    print("electric_paths:")
    for node in G_electric.nodes():
        print(f"节点: {node}")
    for u, v, attr in G_electric.edges(data=True):
        print(f"边: {u}-{v}, 属性: {attr}")

    # # Step 2: 仅添加电链路边
    # electric_edges = [
    #     (u, v) for u, v, attr in G.edges(data=True)
    #     if attr.get('type') == 'electric'
    # ]
    # G_electric.add_edges_from(electric_edges)
    # 输出所有添加的电链路边
    # print(f"electric_paths: {electric_edges}")
    # for edge in electric_edges:
    #     print(f"添加电链路边: {edge}")

    # Step 3: 检查节点是否存在
    if not G_electric.has_node(s) or not G_electric.has_node(d):
        print(f"节点 {s} 或 {d} 在电链路子图中不存在")
        return []

    # Step 4: 搜索路径
    try:
        paths = nx.shortest_simple_paths(G_electric, s, d, weight='hops')
        candidate_paths = [p for _, p in zip(range(k_paths), paths)]
        return candidate_paths
    except nx.NetworkXNoPath:
        return []

topo=build_hybrid_topology_shared_tor(num_tor=4)
paths = find_electric_paths(topo, 'ToR1', 'ToR2', 5)
print(paths)
